{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原创教程，版权所有。\n",
    "\n",
    "同济子豪兄B站视频专栏：https://space.bilibili.com/1900783\n",
    "\n",
    "玩转UCI心脏病二分类数据集，课件、代码、答疑互动：https://t.zsxq.com/Z7yNZBu\n",
    "\n",
    "子豪兄Python交流QQ群：1077638784\n",
    "\n",
    "子豪兄Kaggle数据科学竞赛交流：481041896\n",
    "\n",
    "微信公众号：人工智能小技巧\n",
    "\n",
    "2020-05-15\n",
    "\n",
    "\n",
    "# 本节概述\n",
    "\n",
    "对uci心脏病数据集进行特征预处理，介绍数据分析中定类、定序、定距、定比四大基本数据类型。\n",
    "\n",
    "将原始数据集中的定类离散特征转为One-Hot-Encoding独热编码，便于后续机器学习模型处理。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入工具包和数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 运行本代码，忽略烦人的红色提示\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"heart.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(303, 14)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>cp</th>\n",
       "      <th>trestbps</th>\n",
       "      <th>chol</th>\n",
       "      <th>fbs</th>\n",
       "      <th>restecg</th>\n",
       "      <th>thalach</th>\n",
       "      <th>exang</th>\n",
       "      <th>oldpeak</th>\n",
       "      <th>slope</th>\n",
       "      <th>ca</th>\n",
       "      <th>thal</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>63</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>145</td>\n",
       "      <td>233</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>150</td>\n",
       "      <td>0</td>\n",
       "      <td>2.3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>130</td>\n",
       "      <td>250</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>187</td>\n",
       "      <td>0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>130</td>\n",
       "      <td>204</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>172</td>\n",
       "      <td>0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>236</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>178</td>\n",
       "      <td>0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>120</td>\n",
       "      <td>354</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>163</td>\n",
       "      <td>1</td>\n",
       "      <td>0.6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \\\n",
       "0   63    1   3       145   233    1        0      150      0      2.3      0   \n",
       "1   37    1   2       130   250    0        1      187      0      3.5      0   \n",
       "2   41    0   1       130   204    0        0      172      0      1.4      2   \n",
       "3   56    1   1       120   236    0        1      178      0      0.8      2   \n",
       "4   57    0   0       120   354    0        1      163      1      0.6      2   \n",
       "\n",
       "   ca  thal  target  \n",
       "0   0     1       1  \n",
       "1   0     2       1  \n",
       "2   0     2       1  \n",
       "3   0     2       1  \n",
       "4   0     2       1  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看各列特征数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age           int64\n",
       "sex           int64\n",
       "cp            int64\n",
       "trestbps      int64\n",
       "chol          int64\n",
       "fbs           int64\n",
       "restecg       int64\n",
       "thalach       int64\n",
       "exang         int64\n",
       "oldpeak     float64\n",
       "slope         int64\n",
       "ca            int64\n",
       "thal          int64\n",
       "target        int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将简写的列名修改为完整的特征名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.columns = ['age', 'sex', 'chest_pain_type', 'resting_blood_pressure', 'cholesterol', 'fasting_blood_sugar', 'rest_ecg', 'max_heart_rate_achieved',\n",
    "       'exercise_induced_angina', 'st_depression', 'st_slope', 'num_major_vessels', 'thalassemia', 'target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>chest_pain_type</th>\n",
       "      <th>resting_blood_pressure</th>\n",
       "      <th>cholesterol</th>\n",
       "      <th>fasting_blood_sugar</th>\n",
       "      <th>rest_ecg</th>\n",
       "      <th>max_heart_rate_achieved</th>\n",
       "      <th>exercise_induced_angina</th>\n",
       "      <th>st_depression</th>\n",
       "      <th>st_slope</th>\n",
       "      <th>num_major_vessels</th>\n",
       "      <th>thalassemia</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>63</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>145</td>\n",
       "      <td>233</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>150</td>\n",
       "      <td>0</td>\n",
       "      <td>2.3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>130</td>\n",
       "      <td>250</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>187</td>\n",
       "      <td>0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>130</td>\n",
       "      <td>204</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>172</td>\n",
       "      <td>0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>120</td>\n",
       "      <td>236</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>178</td>\n",
       "      <td>0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>120</td>\n",
       "      <td>354</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>163</td>\n",
       "      <td>1</td>\n",
       "      <td>0.6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age  sex  chest_pain_type  resting_blood_pressure  cholesterol  \\\n",
       "0   63    1                3                     145          233   \n",
       "1   37    1                2                     130          250   \n",
       "2   41    0                1                     130          204   \n",
       "3   56    1                1                     120          236   \n",
       "4   57    0                0                     120          354   \n",
       "\n",
       "   fasting_blood_sugar  rest_ecg  max_heart_rate_achieved  \\\n",
       "0                    1         0                      150   \n",
       "1                    0         1                      187   \n",
       "2                    0         0                      172   \n",
       "3                    0         1                      178   \n",
       "4                    0         1                      163   \n",
       "\n",
       "   exercise_induced_angina  st_depression  st_slope  num_major_vessels  \\\n",
       "0                        0            2.3         0                  0   \n",
       "1                        0            3.5         0                  0   \n",
       "2                        0            1.4         2                  0   \n",
       "3                        0            0.8         2                  0   \n",
       "4                        1            0.6         2                  0   \n",
       "\n",
       "   thalassemia  target  \n",
       "0            1       1  \n",
       "1            2       1  \n",
       "2            2       1  \n",
       "3            2       1  \n",
       "4            2       1  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 区分 定类 定序 定距 定比 四种特征"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "| 特征类型           | 描述                                                         | 举例                                 | 运算             |\n",
    "| ------------------ | ------------------------------------------------------------ | ------------------------------------ | ---------------- |\n",
    "| 定类 Norminal Data | 离散值                                                   | 颜色（红、黄、蓝），性别（男、女）   | 仅可判断是否相等 |\n",
    "| 定序 Ordinal Data  | 离散值，但有顺序                                         | 学历（大学、高中、初中、小学、文盲） | 定类运算+排序    |\n",
    "| 定距 Interval Data | 连续值，可以比较大小，但倍数无可比性，数值为0不代表真正零点                | 摄氏度、天文星等、地震震级           | 定序运算+加减    |\n",
    "| 定比 Ratio Data    | 连续值，倍数有可比性。数值为0代表真正零点 | 年龄、体重、工资、开尔文温度               | 定距运算+乘除    |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将定类特征由整数编码转为实际对应的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['sex'][df['sex'] == 0] = 'female'\n",
    "df['sex'][df['sex'] == 1] = 'male'\n",
    "\n",
    "df['chest_pain_type'][df['chest_pain_type'] == 0] = 'typical angina'\n",
    "df['chest_pain_type'][df['chest_pain_type'] == 1] = 'atypical angina'\n",
    "df['chest_pain_type'][df['chest_pain_type'] == 2] = 'non-anginal pain'\n",
    "df['chest_pain_type'][df['chest_pain_type'] == 3] = 'asymptomatic'\n",
    "\n",
    "df['fasting_blood_sugar'][df['fasting_blood_sugar'] == 0] = 'lower than 120mg/ml'\n",
    "df['fasting_blood_sugar'][df['fasting_blood_sugar'] == 1] = 'greater than 120mg/ml'\n",
    "\n",
    "df['rest_ecg'][df['rest_ecg'] == 0] = 'normal'\n",
    "df['rest_ecg'][df['rest_ecg'] == 1] = 'ST-T wave abnormality'\n",
    "df['rest_ecg'][df['rest_ecg'] == 2] = 'left ventricular hypertrophy'\n",
    "\n",
    "df['exercise_induced_angina'][df['exercise_induced_angina'] == 0] = 'no'\n",
    "df['exercise_induced_angina'][df['exercise_induced_angina'] == 1] = 'yes'\n",
    "\n",
    "df['st_slope'][df['st_slope'] == 0] = 'upsloping'\n",
    "df['st_slope'][df['st_slope'] == 1] = 'flat'\n",
    "df['st_slope'][df['st_slope'] == 2] = 'downsloping'\n",
    "\n",
    "df['thalassemia'][df['thalassemia'] == 0] = 'unknown'\n",
    "df['thalassemia'][df['thalassemia'] == 1] = 'normal'\n",
    "df['thalassemia'][df['thalassemia'] == 2] = 'fixed defect'\n",
    "df['thalassemia'][df['thalassemia'] == 3] = 'reversable defect'\n",
    "# df['target'][df['target'] == 0] = 'No Heart Disease'\n",
    "# df['target'][df['target'] == 1] = 'Heart Disease'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>chest_pain_type</th>\n",
       "      <th>resting_blood_pressure</th>\n",
       "      <th>cholesterol</th>\n",
       "      <th>fasting_blood_sugar</th>\n",
       "      <th>rest_ecg</th>\n",
       "      <th>max_heart_rate_achieved</th>\n",
       "      <th>exercise_induced_angina</th>\n",
       "      <th>st_depression</th>\n",
       "      <th>st_slope</th>\n",
       "      <th>num_major_vessels</th>\n",
       "      <th>thalassemia</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>63</td>\n",
       "      <td>male</td>\n",
       "      <td>asymptomatic</td>\n",
       "      <td>145</td>\n",
       "      <td>233</td>\n",
       "      <td>greater than 120mg/ml</td>\n",
       "      <td>normal</td>\n",
       "      <td>150</td>\n",
       "      <td>no</td>\n",
       "      <td>2.3</td>\n",
       "      <td>upsloping</td>\n",
       "      <td>0</td>\n",
       "      <td>normal</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>male</td>\n",
       "      <td>non-anginal pain</td>\n",
       "      <td>130</td>\n",
       "      <td>250</td>\n",
       "      <td>lower than 120mg/ml</td>\n",
       "      <td>ST-T wave abnormality</td>\n",
       "      <td>187</td>\n",
       "      <td>no</td>\n",
       "      <td>3.5</td>\n",
       "      <td>upsloping</td>\n",
       "      <td>0</td>\n",
       "      <td>fixed defect</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41</td>\n",
       "      <td>female</td>\n",
       "      <td>atypical angina</td>\n",
       "      <td>130</td>\n",
       "      <td>204</td>\n",
       "      <td>lower than 120mg/ml</td>\n",
       "      <td>normal</td>\n",
       "      <td>172</td>\n",
       "      <td>no</td>\n",
       "      <td>1.4</td>\n",
       "      <td>downsloping</td>\n",
       "      <td>0</td>\n",
       "      <td>fixed defect</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>56</td>\n",
       "      <td>male</td>\n",
       "      <td>atypical angina</td>\n",
       "      <td>120</td>\n",
       "      <td>236</td>\n",
       "      <td>lower than 120mg/ml</td>\n",
       "      <td>ST-T wave abnormality</td>\n",
       "      <td>178</td>\n",
       "      <td>no</td>\n",
       "      <td>0.8</td>\n",
       "      <td>downsloping</td>\n",
       "      <td>0</td>\n",
       "      <td>fixed defect</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57</td>\n",
       "      <td>female</td>\n",
       "      <td>typical angina</td>\n",
       "      <td>120</td>\n",
       "      <td>354</td>\n",
       "      <td>lower than 120mg/ml</td>\n",
       "      <td>ST-T wave abnormality</td>\n",
       "      <td>163</td>\n",
       "      <td>yes</td>\n",
       "      <td>0.6</td>\n",
       "      <td>downsloping</td>\n",
       "      <td>0</td>\n",
       "      <td>fixed defect</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age     sex   chest_pain_type  resting_blood_pressure  cholesterol  \\\n",
       "0   63    male      asymptomatic                     145          233   \n",
       "1   37    male  non-anginal pain                     130          250   \n",
       "2   41  female   atypical angina                     130          204   \n",
       "3   56    male   atypical angina                     120          236   \n",
       "4   57  female    typical angina                     120          354   \n",
       "\n",
       "     fasting_blood_sugar               rest_ecg  max_heart_rate_achieved  \\\n",
       "0  greater than 120mg/ml                 normal                      150   \n",
       "1    lower than 120mg/ml  ST-T wave abnormality                      187   \n",
       "2    lower than 120mg/ml                 normal                      172   \n",
       "3    lower than 120mg/ml  ST-T wave abnormality                      178   \n",
       "4    lower than 120mg/ml  ST-T wave abnormality                      163   \n",
       "\n",
       "  exercise_induced_angina  st_depression     st_slope  num_major_vessels  \\\n",
       "0                      no            2.3    upsloping                  0   \n",
       "1                      no            3.5    upsloping                  0   \n",
       "2                      no            1.4  downsloping                  0   \n",
       "3                      no            0.8  downsloping                  0   \n",
       "4                     yes            0.6  downsloping                  0   \n",
       "\n",
       "    thalassemia  target  \n",
       "0        normal       1  \n",
       "1  fixed defect       1  \n",
       "2  fixed defect       1  \n",
       "3  fixed defect       1  \n",
       "4  fixed defect       1  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'sex', 'chest_pain_type', 'resting_blood_pressure',\n",
       "       'cholesterol', 'fasting_blood_sugar', 'rest_ecg',\n",
       "       'max_heart_rate_achieved', 'exercise_induced_angina', 'st_depression',\n",
       "       'st_slope', 'num_major_vessels', 'thalassemia', 'target'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age                          int64\n",
       "sex                         object\n",
       "chest_pain_type             object\n",
       "resting_blood_pressure       int64\n",
       "cholesterol                  int64\n",
       "fasting_blood_sugar         object\n",
       "rest_ecg                    object\n",
       "max_heart_rate_achieved      int64\n",
       "exercise_induced_angina     object\n",
       "st_depression              float64\n",
       "st_slope                    object\n",
       "num_major_vessels            int64\n",
       "thalassemia                 object\n",
       "target                       int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在pandas中，\n",
    "\n",
    "离散的定类和定序特征列应该是`object`这样的对象类型。\n",
    "\n",
    "连续的定距和定比特征列应该是`int64`或者`float64`这样的浮点数类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将离散的定类和定序特征列转为One-Hot独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.get_dummies(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'resting_blood_pressure', 'cholesterol',\n",
       "       'max_heart_rate_achieved', 'st_depression', 'num_major_vessels',\n",
       "       'target', 'sex_female', 'sex_male', 'chest_pain_type_asymptomatic',\n",
       "       'chest_pain_type_atypical angina', 'chest_pain_type_non-anginal pain',\n",
       "       'chest_pain_type_typical angina',\n",
       "       'fasting_blood_sugar_greater than 120mg/ml',\n",
       "       'fasting_blood_sugar_lower than 120mg/ml',\n",
       "       'rest_ecg_ST-T wave abnormality',\n",
       "       'rest_ecg_left ventricular hypertrophy', 'rest_ecg_normal',\n",
       "       'exercise_induced_angina_no', 'exercise_induced_angina_yes',\n",
       "       'st_slope_downsloping', 'st_slope_flat', 'st_slope_upsloping',\n",
       "       'thalassemia_fixed defect', 'thalassemia_normal',\n",
       "       'thalassemia_reversable defect', 'thalassemia_unknown'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>resting_blood_pressure</th>\n",
       "      <th>cholesterol</th>\n",
       "      <th>max_heart_rate_achieved</th>\n",
       "      <th>st_depression</th>\n",
       "      <th>num_major_vessels</th>\n",
       "      <th>target</th>\n",
       "      <th>sex_female</th>\n",
       "      <th>sex_male</th>\n",
       "      <th>chest_pain_type_asymptomatic</th>\n",
       "      <th>...</th>\n",
       "      <th>rest_ecg_normal</th>\n",
       "      <th>exercise_induced_angina_no</th>\n",
       "      <th>exercise_induced_angina_yes</th>\n",
       "      <th>st_slope_downsloping</th>\n",
       "      <th>st_slope_flat</th>\n",
       "      <th>st_slope_upsloping</th>\n",
       "      <th>thalassemia_fixed defect</th>\n",
       "      <th>thalassemia_normal</th>\n",
       "      <th>thalassemia_reversable defect</th>\n",
       "      <th>thalassemia_unknown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>63</td>\n",
       "      <td>145</td>\n",
       "      <td>233</td>\n",
       "      <td>150</td>\n",
       "      <td>2.3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>130</td>\n",
       "      <td>250</td>\n",
       "      <td>187</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41</td>\n",
       "      <td>130</td>\n",
       "      <td>204</td>\n",
       "      <td>172</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>56</td>\n",
       "      <td>120</td>\n",
       "      <td>236</td>\n",
       "      <td>178</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>57</td>\n",
       "      <td>120</td>\n",
       "      <td>354</td>\n",
       "      <td>163</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   age  resting_blood_pressure  cholesterol  max_heart_rate_achieved  \\\n",
       "0   63                     145          233                      150   \n",
       "1   37                     130          250                      187   \n",
       "2   41                     130          204                      172   \n",
       "3   56                     120          236                      178   \n",
       "4   57                     120          354                      163   \n",
       "\n",
       "   st_depression  num_major_vessels  target  sex_female  sex_male  \\\n",
       "0            2.3                  0       1           0         1   \n",
       "1            3.5                  0       1           0         1   \n",
       "2            1.4                  0       1           1         0   \n",
       "3            0.8                  0       1           0         1   \n",
       "4            0.6                  0       1           1         0   \n",
       "\n",
       "   chest_pain_type_asymptomatic  ...  rest_ecg_normal  \\\n",
       "0                             1  ...                1   \n",
       "1                             0  ...                0   \n",
       "2                             0  ...                1   \n",
       "3                             0  ...                0   \n",
       "4                             0  ...                0   \n",
       "\n",
       "   exercise_induced_angina_no  exercise_induced_angina_yes  \\\n",
       "0                           1                            0   \n",
       "1                           1                            0   \n",
       "2                           1                            0   \n",
       "3                           1                            0   \n",
       "4                           0                            1   \n",
       "\n",
       "   st_slope_downsloping  st_slope_flat  st_slope_upsloping  \\\n",
       "0                     0              0                   1   \n",
       "1                     0              0                   1   \n",
       "2                     1              0                   0   \n",
       "3                     1              0                   0   \n",
       "4                     1              0                   0   \n",
       "\n",
       "   thalassemia_fixed defect  thalassemia_normal  \\\n",
       "0                         0                   1   \n",
       "1                         1                   0   \n",
       "2                         1                   0   \n",
       "3                         1                   0   \n",
       "4                         1                   0   \n",
       "\n",
       "   thalassemia_reversable defect  thalassemia_unknown  \n",
       "0                              0                    0  \n",
       "1                              0                    0  \n",
       "2                              0                    0  \n",
       "3                              0                    0  \n",
       "4                              0                    0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(303, 27)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age                                           63.0\n",
       "resting_blood_pressure                       145.0\n",
       "cholesterol                                  233.0\n",
       "max_heart_rate_achieved                      150.0\n",
       "st_depression                                  2.3\n",
       "num_major_vessels                              0.0\n",
       "target                                         1.0\n",
       "sex_female                                     0.0\n",
       "sex_male                                       1.0\n",
       "chest_pain_type_asymptomatic                   1.0\n",
       "chest_pain_type_atypical angina                0.0\n",
       "chest_pain_type_non-anginal pain               0.0\n",
       "chest_pain_type_typical angina                 0.0\n",
       "fasting_blood_sugar_greater than 120mg/ml      1.0\n",
       "fasting_blood_sugar_lower than 120mg/ml        0.0\n",
       "rest_ecg_ST-T wave abnormality                 0.0\n",
       "rest_ecg_left ventricular hypertrophy          0.0\n",
       "rest_ecg_normal                                1.0\n",
       "exercise_induced_angina_no                     1.0\n",
       "exercise_induced_angina_yes                    0.0\n",
       "st_slope_downsloping                           0.0\n",
       "st_slope_flat                                  0.0\n",
       "st_slope_upsloping                             1.0\n",
       "thalassemia_fixed defect                       0.0\n",
       "thalassemia_normal                             1.0\n",
       "thalassemia_reversable defect                  0.0\n",
       "thalassemia_unknown                            0.0\n",
       "Name: 0, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将处理好的数据集导出为csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('process_heart.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
